/**
 * 
 */
package test.machine;

/**
 * 数组转换为机器表示法
 * @author 80002165 @date 2017年4月7日 上午10:15:26
 */
public class NumberToMachine {
    public static void main(String[] args) {
//        NumberToMachine v = new NumberToMachine() ;
//        PrintBinary s= v.new PrintBinary();
//        String result = s.printBinaryInt(10) ;
//        System.out.println(result);
//        System.out.println(result.length()) ;
////        s.printBinaryInt(~256);
        System.out.println(Integer.toBinaryString(10)) ;
//        System.out.println(Integer.parseInt("10", 2)) ;
        
        int zeros = numberOfLeadingZeros(10) ;//1010
//        System.out.println(zeros) ;
        System.out.println(Integer.toBinaryString(-10)) ;
        System.out.println(Integer.numberOfLeadingZeros(-10)) ;
        System.out.println(Integer.numberOfTrailingZeros(-10)) ;
        
    }
    
    public static int numberOfLeadingZeros(int i) {
        // HD, Figure 5-6
        if (i == 0)
            return 32;
        int n = 1;
        if (i >>> 16 == 0) { n += 16; i <<= 16; }
        if (i >>> 24 == 0) { n +=  8; i <<=  8; }
        if (i >>> 28 == 0) { n +=  4; i <<=  4; }
        if (i >>> 30 == 0) { n +=  2; i <<=  2; }
        n -= i >>> 31;
        return n;
    }
    
    class PrintBinary {
        String printBinaryInt(int i){
            StringBuilder result = new StringBuilder() ;
           for(int j=31;j>=0;j--){
               if(((1<<j)& i)!=0)
                   result.append("1") ;
               else
                   result.append("0") ;
           }
           return result.toString() ;    
        }
     }
}
